package leecode

func continuousSubarrays(nums []int) int64 {
	hashMap := make(map[int]int)
	var result int
	left := 0
	for right, num := range nums {
		hashMap[num]++
		for {
			maxNum, minNum := num, num
			for n, _ := range hashMap {
				maxNum = max(maxNum, n)
				minNum = min(minNum, n)
			}
			if maxNum-minNum <= 2 {
				//只要符合条件 , 则结束循环
				break
			}
			if hashMap[nums[left]]--; hashMap[nums[left]] < 0 {
				delete(hashMap, nums[left])
			}
			left++
		}
		result += right - left + 1
	}
	return int64(result)
}
